<?php
namespace Admin\Controller;
use think\controller;
class LinksController extends AuthController{

    public function index()
    {
        $this->page_title="ئۇلىنىش باشقۇرۇش";
        $this->links= M('links')->select();
        $this->display();
    }
    //添加友情链接
    public function add()
    {
        $this->page_title="ئۇلىنىش قوشۇش";
        $this->display();
    }

    //提交
    public function addhend()
    {
        //数据验证
        $rules=[
            ['lname','require','连接名称必须填！'],
            ['lname','','连接名称不能重复！',0,'unique',1],
            ['lurl','require','连接地址必须填！'],
            ['lurl','url','链接地址不合法！']
        ];
        if(!M('links')->validate($rules)->create()) $this->error(M('links')->getError());
        if(!M('links')->add(I('post.'))) $this->error('添加失败！');
        $this->success('添加成功',U('Links/index'),1);
    }

    //删除友情链接
    public function deletelinks($id)
    {
        if(M('links')->where(array('id'=>$id))->delete()){
            $data= ['status'=>1,'message'=>'ئۆچۈرۈش تامام'];
        }
        else{
            $data= ['status'=>0,'message'=>'ئۆچۈرۈش مەغلۇب بولدى!'];
        }
        $this->ajaxReturn($data);
    }

    //编辑友情链接
    public function edit($id)
    {
        $this->page_title="ئۇلىنىش تەھرىرلەش";
        $this->links=M('links')->where(array('id'=>$id))->find();
        $this->display();
    }

    //提交友情链接表单
    public function savehend()
    {
        if(!M('links')->save(I('post.'))) $this->error('编辑失败!');
        $this->success('编辑成功',U('Links/index'),1);
    }
}